package study;

import org.junit.jupiter.api.Test;
import tool.s;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class streamDemo {


/*
 遍历 匹配 规约 聚合
 foreach find match reduce max min count
 筛选 映射 收集 排序
 filter map collect sorted
*/


    int[] arr;
    List<Integer> list;
    Set<Integer> set;

    public void between() {
        arr = new int[]{1, 2, 3, 3, 2, 1};
        list = Arrays.asList(1, 2, 3, 4, 5, 1, 2);
        set = new HashSet<>(list);
    }

    public void forEach_find_match() {
        //遍历、匹配
        //forEach
        list.stream().forEach(System.out::print);
        System.out.println();
        //find
        int a = list.stream().findFirst().get();
        System.out.println(a);
        //match
        boolean b=list.stream().anyMatch(x -> x > 4);
        System.out.println(b);
    }
    public void filter(){
        //筛选
        List<Integer> list1=list.stream().filter(x->x>3).collect(Collectors.toList());
        System.out.println(list1);
    }
    public void max_min_count(){
        //聚合
        //max
        int x=list.stream().max((a,b)->a-b).get();
        System.out.println(x);
        //min
        x=list.stream().min((a,b)->a-b).get();
        System.out.println(x);
        //count
        long l=list.stream().count();
        System.out.println(l);
    }
    public void map_flatMap(){
        //映射
        //map
        List<Integer> list1=list.stream().map(a->a+10).collect(Collectors.toList());
        System.out.println(list1);
        //flatMap
        list1=list.stream().flatMap(a->{
            return new ArrayList<Integer>(){{add(a);add(a+10);add(a+20);}}.stream();
        }).collect(Collectors.toList());
        System.out.println(list1);
    }
    public void reduce(){
        //规约
        //reduce
        Optional<Integer> reduce = list.stream().reduce((a, b) -> a + b);
        System.out.println(reduce.get());
        reduce = list.stream().reduce((a, b) -> a * b);
        System.out.println(reduce.get());
        reduce = list.stream().reduce((a, b) -> a>b?a:b);
        System.out.println(reduce.get());
    }
    public void collect(){
        //收集
        //collect(Collectors.toList)
        //collect(Collectors.toSet)
        //collect(Collectors.toMap)
        Map<Integer,Integer> map=set.stream().collect(Collectors.toMap(a->a,a->a*a));
        System.out.println(map);
        //collect(Collectors.counting())
        long l=list.stream().collect(Collectors.counting());
        System.out.println(l);
        //collect(Collectors.averagingInt(a->a))
        Double d=list.stream().collect(Collectors.averagingInt(a->a));
        System.out.println(d);
        //collect(Collectors.maxBy(Integer::compare))
        Optional<Integer> o=list.stream().collect(Collectors.maxBy(Integer::compare));
        System.out.println(o.get());
        int sum=list.stream().collect(Collectors.summingInt(a->(int)a));
        System.out.println(sum);
        //collect(Collectors.summarizingInt(a->(int)a))
        IntSummaryStatistics sta=list.stream().collect(Collectors.summarizingInt(a->(int)a));
        System.out.println(sta.getSum()+" "+sta.getMax()+" "+sta.getMin()+" "+sta.getCount()+" "+sta.getAverage());
    }
    public void partitioningBy_groupingBy(){
        //分类和分组
        //partitioningBy
        Map<Boolean, List<Integer>> map=list.stream().collect(Collectors.partitioningBy(a->a>2));
        System.out.println(map);
        //groupingBy
        Map<Integer, List<Integer>> map1=list.stream().collect(Collectors.groupingBy(a->a%3));
        System.out.println(map1);
    }
    public void joining(){
        //连接为字符串
        String s=list.stream().map(a->Integer.toString(a)).collect(Collectors.joining("-"));
        System.out.println(s);
    }
    public void sorted(){
        //排序
        List<Integer> list1=list.stream().sorted(Comparator.comparing(a->(int)a).reversed()).collect(Collectors.toList());
        System.out.println(list1);
    }

    @Test
    public void test() {
        between();
//        s.print(arr);
//        s.print(list.toArray());
//        s.print(set.toArray());
//        forEach_find_match();


//        filter();
//        max_min_count();
//        map_flatMap();
//        reduce();
//        collect();
//        partitioningBy_groupingBy();
//        joining();
        sorted();

    }
}
